import { addLeave, updateLeave } from "@/api/flandre-ui/request";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { store } from "@/redux";
let reload: Function;
let leaveId: String;
let user: any;
const timeFn = (time: string) => {
	if (!time) {
		return "";
	}
	const [date] = time.split(" ");
	return date;
};
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "HRMS.leaveApproval";
	const formSchema: FormSchema[] = [
		{
			label: t("felix_ui.staff_id"),
			field: "staffId", //科人员ID
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.startDate`),
			field: "startDate", //开始时间
			component: "DatePicker",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.endDate`),
			field: "endDate", //结束时间
			component: "DatePicker",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.leaveType`),
			field: "leaveType", //请假类型：1——请假，2——加班，3——调休，3——出差，5——外出,示例值(2)
			component: "Select",
			componentProps: {
				options: [
					{ label: "Leave", value: 1 },
					{ label: "Overtime", value: 2 },
					{ label: "Compensatory Leave", value: 3 },
					{ label: "Business Trip", value: 4 },
					{ label: "Outing", value: 5 }
				],
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.reason`),
			field: "leaveReason", //请假原因,
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t("kevin_ui.status"),
			field: "status", //员工状态
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.approver`),
			field: "approver", //审批人
			component: "Input",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.approvalTime`),
			field: "approvalTime", //审批时间
			component: "DatePicker",
			componentProps: {
				disabled: true
			}
		},
		{
			label: t(`${en_vi}.didItPass`),
			field: "didItPass",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`yes`), value: true },
					{ label: t(`no`), value: false }
				]
			}
		},
		{
			label: t(`${en_vi}.approvalOpinion`),
			field: "approvalOpinion", //审批意见
			component: "Input"
		}
	];

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		user = store.getState().user.userInfo.user;
		resetFields();
		reload = data.reload;
		if (data.record) {
			leaveId = data.record.leaveId;
			data.record = {
				...data.record,
				approver: user.nickname,
				approvalTime: new Date()
			};
			setFieldsValue(data.record);
		}
	});
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const handleSubmit = async () => {
		let formValue = await validateFields();
		formValue.startDate = timeFn(formValue.startDate);
		formValue.endDate = timeFn(formValue.endDate);
		if (leaveId) {
			const res = await updateLeave({ ...formValue, leaveId });
			closeDrawer();
			reload();
			res ? message.success(t("kevin_ui.updateSuccess")) : message.error(t("kevin_ui.updateFailed"));
			return;
		}
		const res = await addLeave(formValue);
		if (!res) {
			message.error(t("kevin_ui.addFailed"));
			return;
		}
		message.success(t("kevin_ui.addedSuccessfully"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer showFooter={true} size="default" {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
